/*
 * Copyright 2007-208 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package com.cassevern.grails.util

class Build400GrailsDomainRRNTest extends GroovyTestCase {
	
	String STOR_PROC =
"""CREATE PROCEDURE dgd.INSERT_alltypes (IN PCKDEC decimal(9,2), IN ZONDEC int, IN FLOAT9 real, IN BIN1 smallint, IN BIN2 smallint, IN BIN4 smallint, IN BIN8 integer, IN BIN16 bigint, IN CHAR20 char(20), IN DATEL date, IN TIMET time, IN TIMSTP timestamp, IN rrnToIgnor int)
LANGUAGE SQL 
BEGIN
INSERT INTO dgd.alltypes (PCKDEC, ZONDEC, FLOAT9, BIN1, BIN2, BIN4, BIN8, BIN16, CHAR20, DATEL, TIMET, TIMSTP)
VALUES(PCKDEC, ZONDEC, FLOAT9, BIN1, BIN2, BIN4, BIN8, BIN16, CHAR20, DATEL, TIMET, TIMSTP);
END"""
		String XML = 
"""<hibernate-mapping>
  <class name='AllTypes' table='alltypes_RRN'>
    <id name='id' type='int'>
      <generator class='assigned' />
    </id>
    <property precision='9' column='PCKDEC' name='packedDecimal' scale='2' type='big_decimal' not-null='true' />
    <property precision='5' column='ZONDEC' name='zonedDecimal' scale='0' type='int' not-null='true' />
    <property precision='7' column='FLOAT9' name='float9' scale='0' type='double' not-null='true' />
    <property precision='5' column='BIN1' name='bin1' scale='0' type='short' not-null='true' />
    <property precision='5' column='BIN2' name='bin2' scale='0' type='short' not-null='true' />
    <property precision='5' column='BIN4' name='bin4' scale='0' type='short' not-null='true' />
    <property precision='10' column='BIN8' name='bin8' scale='0' type='int' not-null='true' />
    <property precision='19' column='BIN16' name='bin16' scale='0' type='long' not-null='true' />
    <property length='20' column='CHAR20' name='char20' type='string' not-null='true' />
    <property precision='10' column='DATEL' name='datel' scale='0' type='date' not-null='true' />
    <property precision='8' column='TIMET' name='timet' scale='0' type='time' not-null='true' />
    <property precision='26' column='TIMSTP' name='timstp' scale='6' type='timestamp' not-null='true' />
    <sql-insert callable='true' check='none'>{ call INSERT_alltypes (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) }</sql-insert>
  </class>
</hibernate-mapping>"""

	def DOMAIN = //
"""class AllTypes implements Serializable {
		int id // relative record number 
		BigDecimal packedDecimal
		int zonedDecimal
		Double float9
		Short bin1
		Short bin2
		Short bin4
		int bin8
		Long bin16
		String char20
		Date datel
		Date timet
		Date timstp
		static constraints = {
			packedDecimal(max:new BigDecimal("9999999.99"),nullable:false)
			zonedDecimal(max:99999,nullable:false)
			float9(nullable:false)
			bin1(nullable:false)
			bin2(nullable:false)
			bin4(nullable:false)
			bin8(nullable:false)
			bin16(nullable:false)
			char20(maxSize:20,nullable:false)
			datel(nullable:false)
			timet(nullable:false)
			timstp(nullable:false)
		}
	}"""

	def VIEW = "create view dgd.alltypes_RRN as select rrn(dbf) as id, dbf.* from dgd.alltypes dbf"
		        
	void testRRNGenerator () { 
		Build400GrailsDomain build = new Build400GrailsDomain(
				ip:'172.16.1.4', user:'dgd', password:'vo2max', library:'dgd', file:'alltypes', domainName:'AllTypes', useRRNasKey:true, verbose:true)
		build.retrieveTableAttributes() 	
		XML = XML.replaceAll(/\t|\r|\n/,' ')
		XML = XML.replaceAll(/\s{2,}/,' ')
		def xml = build.writeHibernateXML ()
		xml = xml.replaceAll(/\t|\r|\n/,' ')
		xml = xml.replaceAll(/\s{2,}/,' ')
		assertEquals (XML, xml)

		STOR_PROC = STOR_PROC.replaceAll(/\t|\r|\n/,' ')
		STOR_PROC = STOR_PROC.replaceAll(/\s{2,}/,' ')
		def storProc = build.createStoredProcedure()
		storProc = storProc.replaceAll(/\t|\r|\n/,' ')
		storProc = storProc.replaceAll(/\s{2,}/,' ')
		assertEquals (STOR_PROC, storProc)
		
		def view = build.createRRNView()
		assertEquals (VIEW, view)
		
		DOMAIN = DOMAIN.replaceAll(/\t|\r|\n/,' ')
		DOMAIN = DOMAIN.replaceAll(/\s{2,}/,' ')
		def domain = build.createDomainClass()
		domain = domain.replaceAll(/\t|\r|\n/,' ')
		domain = domain.replaceAll(/\s{2,}/,' ')
		assertEquals (DOMAIN, domain)
		
	}
}
